<?php
namespace PsiFramework\Classes\Model;

import("Classes.Model.Object");

use \PsiFramework\Classes\Model\Object;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe abstraite d'Objet Polymorphique
 *
 * @package Model
 */
abstract class PolymorphicObject extends Object
{

    public function __call($methodName, $args)
    {
        if (preg_match('~^(set|get)([A-Z])(.*)$~', $methodName, $matches)) {
            $property = strtolower($matches[2]) . $matches[3];
            switch($matches[1]) {
            case 'set':
                if (count($args) == 1) {
                    return $this->set($property, $args[0]);
                } else {
                    throw new \Exception('The setter needs one argument !');
                }
                break;
            case 'get':
                if (count($args) == 0) {
                    return $this->get($property);
                } else {
                    throw new \Exception(
                        'The setter doesnt sypport any argument !'
                    );
                }
                break;
            case 'default':
                throw new \Exception('Method not supported !');
                break;
            }
        }

        parent::__call($methodName, $args);
    }

    protected function get($property)
    {
        if (!isset($this->$property)) {
            return null ;
        } else {
            return $this->$property;
        }
    }

    protected function set($property, $value)
    {
        $this->$property = $value;
    }

}
